package com.jnu.mydiary.calendar;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jnu.mydiary.AddActivity;
import com.jnu.mydiary.R;
import com.jnu.mydiary.ui.shouye1.DashboardFragment;
import com.jnu.mydiary.ui.shouye1.DataBank;
import com.jnu.mydiary.ui.shouye1.myBean;

import java.util.List;

/**
 *  日历 适配器
 * Created by aiyang on 2017/4/14.
 */

public class RegistrationAdapter extends BaseAdapter {
    private Context context;
    private final int days;
    private final int week;
    private int[] dayNumber;
    private final int day;
    private String select_mood=null;
    private int now_day=0;

    public RegistrationAdapter(Context context, int days, int week,int day) {
        this.context = context;
        this.days = days;
        this.week = week;
        this.day=day;
        getEveryDay();
    }

    public void setSelect_mood(String select_mood) {
        this.select_mood = select_mood;
    }

    @Override
    public int getCount() {
        return 42;
    }

    @Override
    public String getItem(int i) {

        return null;
    }

    @Override
    public long getItemId(int i) {
        return dayNumber[i];
    }//点击时

    private ViewHolder viewHolder;
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (null==view){
            view= LayoutInflater.from(context).inflate(R.layout.item_registrationadatper,null);
            viewHolder=new ViewHolder(view);
            view.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) view.getTag();
        }
        viewHolder.day.setText(dayNumber[i]==0? "" : dayNumber[i]+"");
        DataBank mDataBank = new DataBank(view.getContext());
        List<myBean> mList = mDataBank.loadData();
        for (int j = 0; j < mList.size(); j++) {
            if (mList.get(j).getDay() == dayNumber[i]) {
                viewHolder.picture.setImageResource(mList.get(j).getmImageId());//实现文件存储打卡日期遍历
            }
        }

        if(now_day!=0){//now_day代表添加的日期是几号
            if(dayNumber[i]==now_day){
                int id_mood=0;
                for(int j=0;j< AddActivity.itemNames_mood.length;j++){
                    if(AddActivity.itemNames_mood[j].equals(select_mood)){
                        id_mood=AddActivity.itemImage_mood[j];
                        break;
                    }
                }
                viewHolder.picture.setImageResource(id_mood);
            }
        }

        if (dayNumber[i]==day){

            viewHolder.day.setText("今");
            viewHolder.day.setTextColor(Color.parseColor("#ffffff"));
            if(true) {
                for (int j = 0; j < mList.size(); j++) {
                    if (mList.get(j).getDay() == day) {
                        viewHolder.picture.setImageResource(mList.get(j).getmImageId());
                    }
                }
            }
            else if(select_mood!=null){
                int id_mood=0;
                for(int j=0;j< AddActivity.itemNames_mood.length;j++){
                    if(AddActivity.itemNames_mood[j].equals(select_mood)){
                        id_mood=AddActivity.itemImage_mood[j];
                        break;
                    }
                }
               viewHolder.picture.setImageResource(id_mood);
            }
            else {
                view.setBackgroundResource(R.color.edit_button);
            }


        }

        return view;
    }

    public void setNow_day(int now_day) {
        this.now_day=now_day;
    }

    private class ViewHolder{
        private TextView day;
        private ImageView picture;

        public ViewHolder(View view) {
            this.day = (TextView) view.findViewById(R.id.day);
            this.picture=(ImageView) view.findViewById(R.id.imageView2);
          }
    }

    /**
     * 得到42格子 每一格子的值
     */
    private void getEveryDay(){
        dayNumber=new int[42];

        for (int i=0;i<42;i++){
            if (i < days+week && i >= week){
                dayNumber[i]=i-week+1;
            }else{
                dayNumber[i]=0;
            }
        }
    }
}